<?php

namespace App\queue\redis;
use app\model\OrderAssignment;
use App\model\Staff;
use GatewayClient\Gateway;
use think\facade\Db;
use Webman\RedisQueue\Consumer;
use EasyWeChat\Factory;
class AppMessage implements Consumer
{
// 要消费的队列名
    public $queue = 'app-message';

    // 连接名，对应 plugin/webman/redis-queue/redis.php 里的连接`
    public $connection = 'default';

    // 消费
    public function consume($data)
    {
        $config=Db::table('wechat_app')->where('id',3)->find();
        $config['corp_id']=$config['app_id'];
        $contacts = Factory::work($config);
        $messenger = $contacts->messenger;
        Gateway::$registerAddress = '127.0.0.1:8101';
        $o=OrderAssignment::with('staff')->select();
        if($o->isEmpty()){
            return;
        }
        $arr=$o->toArray();
        foreach ($arr as $key=>$item){

            if(is_array($item['staff'])){
                foreach ($item['staff'] as $i){
                    if(isset($i['userid'])){
                        $message = new \EasyWeChat\Kernel\Messages\TextCard([
                            'title' => '10月线上销售报表请查收（测试中，数据以实际为准）',
                            'description' => $item['shop_name'].'10月线上销售任务'.$item['sales_target'].',现已完成'.$item['sales'].'本数据为自动测试数据，综合完成率'.$item['proportion'],
                            'url' => 'https://webman.wyx2013.cn/work/index/click?id='.$i['staff_id']
                        ]);
                        $r=$messenger->message($message)->toUser($i['userid'])->send();
                        //Gateway::sendToUid('admin',json_encode($key));
                    }
                }
            }
        }
        Staff::where('staff_id','>',0)->update(['open'=>0]);
        Gateway::sendToUid('admin',json_encode(['type'=>'message','code'=>0,'message'=>'推送成功']));
    }
}